/**
 * 
 */
package com.tech.freak.pyp.data;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author julian
 *
 */
public class PyPContentProvider extends ContentProvider {
	
	// Log Tag
	private static final String TAG = "PyPContentProvider";

	/*
	 * 
	 * Database INFO
	 */
	protected static final String DATABASE_NAME = "pyp.db";
	protected static final int DATABASE_VERSION = 1;
	
	private static HashMap<String, String> PyPProjectionMap;
	
	// picoyplaca table
	private static final int PYP = 1;
	private static final int PYP_ID = 2;
	
	// Uri Matcher
	private static final UriMatcher sUriMatcher;
		
	// Instance of the database open helper
	private PicoyPlacaDatabasehelper mDbHelper;

	private Context mContext;

	
	/* (non-Javadoc)
	 * @see com.tech.freak.pyp.data.SQLiteContentProvider#onCreate()
	 */
	@Override
	public boolean onCreate() {
		Log.i(TAG, "onCreate");
		mContext = getContext();
		mDbHelper = PicoyPlacaDatabasehelper.getInstance(mContext);

		return true;
	}

	/* (non-Javadoc)
	 * @see com.tech.freak.pyp.data.SQLiteContentProvider#insertInTransaction(android.net.Uri, android.content.ContentValues)
	 */
	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		long id = 0;
		mDbHelper.getWritableDatabase();
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}

		int match = sUriMatcher.match(uri);
		// Validate the requested uri
		switch (match) {
		case PYP:
			id = mDbHelper.pypInsert(values);
			break;
		default:
			throw new IllegalArgumentException("Unsupported URI " + uri);
		}
		
		if (id > 0) {
			Uri newuri = ContentUris.withAppendedId(Uri.parse("content://"
					+ PicoyPlaca.AUTHORITY + "/" + PicoyPlaca.PYP_TABLE_NAME), id);
			mContext.getContentResolver().notifyChange(newuri, null);
			
			return newuri;
		}else {
			throw new IllegalArgumentException(
					"Error inserting item with uri: " + uri);
		}
	}

	/* (non-Javadoc)
	 * @see com.tech.freak.pyp.data.SQLiteContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
	 */
	@Override
	public int update(Uri uri, ContentValues values,
			String selection, String[] selectionArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count;
		
		int match = sUriMatcher.match(uri);
		// Validate the requested uri
		switch (match) {
		case PYP:
			count = db.update(PicoyPlaca.PYP_TABLE_NAME, values, selection,
					selectionArgs);
			break;
			
		case PYP_ID:
			String element_id = uri.getPathSegments().get(1);
			count = db.update(PicoyPlaca.PYP_TABLE_NAME, values,
					"_id="
							+ element_id
							+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
									+ ')' : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unsupported URI " + uri);
		}
		mContext.getContentResolver().notifyChange(uri, null);
		return count;
	}

	/* (non-Javadoc)
	 * @see com.tech.freak.pyp.data.SQLiteContentProvider#deleteInTransaction(android.net.Uri, java.lang.String, java.lang.String[])
	 */
	@Override
	public int delete(Uri uri, String selection,
			String[] selectionArgs) {
		Log.i(TAG, "delete");
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = 0;

		int match = sUriMatcher.match(uri);
		// Validate the requested uri
		switch (match) {
		case PYP:
			count = db.delete(PicoyPlaca.PYP_TABLE_NAME, selection, selectionArgs);
			break;
		case PYP_ID:
			String element_id = uri.getPathSegments().get(1);
			count = db.delete(PicoyPlaca.PYP_TABLE_NAME,
					"_id="
							+ element_id
							+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
									+ ')' : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unsupported URI " + uri);
		}
		mContext.getContentResolver().notifyChange(uri, null);
		return count;
	}

	protected void notifyChange() {
		// Note that semantics are changed: notification is for CONTENT_URI, not
		// the specific
		// Uri that was modified.
		mContext
				.getContentResolver()
				.notifyChange(PicoyPlaca.CONTENT_URI, null, false /* syncToNetwork */);

	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#getType(android.net.Uri)
	 */
	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Log.i(TAG, "query");

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		
		switch (sUriMatcher.match(uri)) {
		case PYP:
			qb.setTables(PicoyPlaca.PYP_TABLE_NAME);
			qb.setProjectionMap(PyPProjectionMap);
			break;
			
		case PYP_ID:
			qb.setTables(PicoyPlaca.PYP_TABLE_NAME);
			qb.setProjectionMap(PyPProjectionMap);
			qb.appendWhere(PicoyPlaca.PYP_TABLE_NAME + "." + PicoyPlaca.PYP._ID + "="
					+ uri.getPathSegments().get(1));
			break;
		default:
			throw new IllegalArgumentException("Unsupported URI " + uri);
		}
		// Get the database and run the query
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);

		// Tell the cursor what uri to watch, so it knows when its source data
		// changes
		c.setNotificationUri(mContext.getContentResolver(), uri);

		return c;
	}

	static{
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		
		// picoyplaca
		sUriMatcher.addURI(PicoyPlaca.AUTHORITY, PicoyPlaca.PYP_TABLE_NAME, PYP);
		sUriMatcher.addURI(PicoyPlaca.AUTHORITY, PicoyPlaca.PYP_TABLE_NAME+"/#", PYP_ID);
		
		// picoyplaca
		PyPProjectionMap = new HashMap<String, String>();
		PyPProjectionMap.put(PicoyPlaca.PYP._ID, PicoyPlaca.PYP._ID);
		PyPProjectionMap.put(PicoyPlaca.PYP.CITY, PicoyPlaca.PYP.CITY);
		PyPProjectionMap.put(PicoyPlaca.PYP.MONDAY, PicoyPlaca.PYP.MONDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.TUESDAY, PicoyPlaca.PYP.TUESDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.WEDNSEDAY, PicoyPlaca.PYP.WEDNSEDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.THURSDAY, PicoyPlaca.PYP.THURSDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.FRIDAY, PicoyPlaca.PYP.FRIDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.SATURDAY, PicoyPlaca.PYP.SATURDAY);
		PyPProjectionMap.put(PicoyPlaca.PYP.SUNDAY, PicoyPlaca.PYP.SUNDAY);
		
	}

}
